package cn.laixueit.cart.controller;

import cn.laixueit.cart.pojo.item.Sku;
import cn.laixueit.cart.pojo.item.Spu;
import cn.laixueit.cart.pojo.item.SpuDetail;
import cn.laixueit.cart.pojo.item.SpuVo;
import cn.laixueit.cart.pojo.page.PageResult;
import cn.laixueit.cart.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class GoodsController {

    @Autowired
    private SpuService spuService;

    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuVo>> page(@RequestParam(value = "key",required = false)String key,
                                                  @RequestParam(value = "saleable",required = false) Boolean saleable,
                                                  @RequestParam(value = "page",required = true)Integer page,
                                                  @RequestParam(value = "rows",required = true)Integer rows){

        PageResult<SpuVo> spuVo = spuService.findPage(key,saleable,page,rows);

        return ResponseEntity.ok(spuVo);
    }


    @PostMapping("/goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuVo spuVo){
        spuService.saveGoods(spuVo);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("/spu/detail/{id}")
    public ResponseEntity<SpuDetail> detail(@PathVariable("id")Long id){
        SpuDetail spuDetail = spuService.findDetailBySpuId(id);
        return ResponseEntity.ok(spuDetail);
    }

    @GetMapping("/sku/list")
    public ResponseEntity<List<Sku>> list(@RequestParam("id")Long id){
        List<Sku> skus = spuService.skuListBySpuId(id);
        return ResponseEntity.ok(skus);
    }

    @GetMapping("/spu/{id}")
    public ResponseEntity<Spu> getSpuById(@PathVariable("id")Long id){
        Spu spu = spuService.getById(id);
        return ResponseEntity.ok(spu);
    }

    @GetMapping("/sku/{id}")
    public ResponseEntity<Sku> getSkuById(@PathVariable("id")Long id){
        Sku sku = spuService.getSkuById(id);
        return ResponseEntity.ok(sku);
    }
}
